package com.supplychainapi.service.impl;

import com.google.common.collect.Lists;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.ScmTbcheckexceptMapper;
import com.supplychainapi.model.ScmTbcheckexcept;
import com.supplychainapi.param.CheckExceptParam;
import com.supplychainapi.service.TbCheckExceptService;
import com.supplychainapi.utils.StringUtil;
import com.xiaoleilu.hutool.util.BeanUtil;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by panic .
 * Time 2017/11/17
 * description: 验收异常服务实现类
 */
@Service
public class TbCheckExceptServiceImpl implements TbCheckExceptService {
    @Autowired
    ScmTbcheckexceptMapper scmTbcheckexceptMapper;

    @Override
    public RestResponse save(CheckExceptParam model) throws ServiceException {
        ScmTbcheckexcept scmTbcheckexcept = new ScmTbcheckexcept();
        BeanUtil.copyProperties(model, scmTbcheckexcept);
        scmTbcheckexcept.setFsCreateTime(new Date());
        scmTbcheckexcept.setFsUpdateTime(new Date());
        if (StringUtils.isBlank(scmTbcheckexcept.getFsCheckExceptId())) {
            scmTbcheckexcept.setFsCheckExceptId(getCode(model.getFsShopGUID()));
        }
        Integer isExit = scmTbcheckexceptMapper.checkExceptIsExit(model.getFsCheckExceptName(), model.getFsShopGUID());

        if (isExit > 0) {
            throw ExceptionMessage.material_name_is_exit.getServiceException();
        }

        int insertSelective = scmTbcheckexceptMapper.insertSelective(scmTbcheckexcept);

        if (insertSelective <= 0) {
            throw ExceptionMessage.operation_failed.getServiceException();
        }
        return RestResponse.success();
    }

    @Override
    public RestResponse delete(List<String> list, String shopGuid) throws ServiceException {
        try {
            if (CollectionUtil.isEmpty(list)) {
                throw ExceptionMessage.missing_param.getServiceException();
            }
            list.stream().forEach(e -> {
                if (StringUtils.isNotBlank(e)) {
                    scmTbcheckexceptMapper.delete(new ScmTbcheckexcept(e, shopGuid));
                }
            });
        } catch (Exception e) {
            throw ExceptionMessage.getSystemException(e);
        }
        return RestResponse.success();
    }

    @Override
    public RestResponse update(CheckExceptParam model) throws ServiceException {
        ScmTbcheckexcept scmTbcheckexcept = new ScmTbcheckexcept();
        BeanUtil.copyProperties(model, scmTbcheckexcept);
        scmTbcheckexcept.setFsUpdateTime(new Date());
        if (scmTbcheckexceptMapper.updateByPrimaryKeySelective(scmTbcheckexcept) <= 0) {
            throw ExceptionMessage.operation_failed.getServiceException();
        }
        return RestResponse.success();
    }

    @Override
    public RestResponse<List<ScmTbcheckexcept>> selectAll(String shopGuid) throws ServiceException {
        ScmTbcheckexcept scmTbcheckexcept = new ScmTbcheckexcept();
        scmTbcheckexcept.setFsShopGUID(shopGuid);
        List<ScmTbcheckexcept> tbcheckexceptList = scmTbcheckexceptMapper.select(scmTbcheckexcept);
        if (CollectionUtil.isEmpty(tbcheckexceptList)) {
            return RestResponse.success(Lists.newArrayList());
        }
        return RestResponse.success(tbcheckexceptList);
    }

    @Override
    public RestResponse<ScmTbcheckexcept> selectById(String shopGuid, String fsCheckExceptId) throws ServiceException {
        ScmTbcheckexcept scmTbcheckexcept = scmTbcheckexceptMapper.selectByPrimaryKey(new ScmTbcheckexcept(fsCheckExceptId, shopGuid));
        if (null == scmTbcheckexcept) {
            return RestResponse.success();
        }
        return RestResponse.success(scmTbcheckexcept);
    }


    /**
     * 查询所有已启用的验收异常
     *
     * @param shopGuid
     * @return
     * @throws ServiceException
     */
    @Override
    public RestResponse<List<ScmTbcheckexcept>> selectActivateAll(String shopGuid) throws ServiceException {
        ScmTbcheckexcept scmTbcheckexcept = new ScmTbcheckexcept();

        scmTbcheckexcept.setFsShopGUID(shopGuid);
        scmTbcheckexcept.setFiStatus((short) ExceptionMessage.is_activate.getCode());

        List<ScmTbcheckexcept> tbcheckexceptList = scmTbcheckexceptMapper.select(scmTbcheckexcept);
        if (CollectionUtil.isEmpty(tbcheckexceptList)) {
            return RestResponse.success(Lists.newArrayList());
        }
        return RestResponse.success(tbcheckexceptList);
    }

    @Override
    public String getCode(String shopGuid) {
        String code = scmTbcheckexceptMapper.getCode(shopGuid);

        if (StringUtils.isBlank(code)) {
            return "1001";
        } else {
            return StringUtil.autoGenericCode(code, code.length());
        }
    }
}
